package com.king.cms.online;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.db.QueryPage;
import com.king.cms.log.LogService;
import com.king.cms.common.*;
 

public class OnlineAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(OnlineAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
		 String action = request.getParameter("action"); 
		 if("list".equals(action)){
			 ServletContext application = request.getSession().getServletContext();
			 Vector activeSessions = (Vector) application.getAttribute("activeSessions");
			 ArrayList users = new ArrayList();
			 if (activeSessions == null)
			 {
			   activeSessions = new Vector();
			   application.setAttribute("activeSessions",activeSessions);
			 }
			 Iterator it = activeSessions.iterator();
			 while (it.hasNext())
			 { 
			   HttpSession sess = (HttpSession)it.next(); 
			   try{ 
				   UserObj sessionUser = (UserObj)sess.getAttribute("user");
				   String userId = (sessionUser!=null)?sessionUser.getUid():"None";
				   List list = this.getDBOperator(request).select(InitSystem.getBpmParam("select.user.sql_byuid"), new Object[]{userId});
				   if(list.size()>0){
					   Map m = (Map)list.get(0);
					   sessionUser.setDuty(m.get("USER_NAME")+"");
					   sessionUser.setUserName(m.get("USER_DUTY")+""); 
				   }
				   users.add(sessionUser);
				  
			   }catch(Exception e){
				   System.out.print("Session is time out");
			   }
			 } 
			 request.setAttribute("users", users);
			 return mapping.findForward("online");
		 }
		 if("history".equals(action)){
			 String sql = "select log.*,user.* from cms_log log,cms_user user where log.LOG_TYPE=3 and  log.LOG_USER = user.ID and (to_days(now())-to_days(LOG_TIME))<7 order by LOG_TIME desc";
			 QueryPage query = new QueryPage(request, request.getContextPath()+"/online.do?action=history" , sql);
			 request.setAttribute("query", query);
			 return mapping.findForward("history");
		 }
		 return null;
	}
	 
}
